﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace MaxLeaf.DAL.W2K
{


    /// <summary>
    /// 注意，实际来源是k3的类别
    /// </summary>
    public class goods_mainGroupDAL : BaseGroupDAL
    {
        
        public goods_mainGroupDAL(string strClassID)
            : base()
        {

           
            strWebTableName = "t_ComCategory";

            strK3MapKeyFieldName = "DestKeyValue";
            strWebMapKeyFieldName = "OrgKeyValue";

            this.strFItemClassID = strClassID;
            if (strFItemClassID.Equals(Const.FItemClassID_ItemGroup_1))
            {
                strWebMapKeyTableName = "mItemGroupKeyMap_1";
                strLogType = LogType.ImInvGroup_1.ToString();
                strK3TableName = "t_ICItem";
            }
            else
            {
                strWebMapKeyTableName = "mItemGroupKeyMap_2";
                strLogType = LogType.ImInvGroup_2.ToString();
                strK3TableName = "cbCostObj";
            }
            //strK3DestRealKeyFieldName = "FName";
            //strWebOrgDestRealKeyFieldName = "FName";

            
            

            strFieldNames = "FNumber,FName";

           
        }

        public override void Delete(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {

            string strDestKeyValue = GetDestKeyValue(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue);
            if (strDestKeyValue.Length == 0)
            {
                return;
            }
            string strSql = string.Empty;

            Dictionary<string, object> dictCond = new Dictionary<string, object>() { { strK3MapKeyFieldName, strDestKeyValue } };
            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(dictCond);
            //string strSql = string.Format(@"delete from {0} where  {1}", strK3TableName, kvp.Key);
            //dbDestK3Con.ExecuteSql(strSql, kvp.Value);

            dictCond = new Dictionary<string, object>() { { "FitemID", strDestKeyValue } };
            kvp = dbDestK3Con.CreateCondParam(dictCond);
            strSql = string.Format(@"delete from {0} where  {1}", "t_item", kvp.Key);
            dbDestK3Con.ExecuteSql(strSql, kvp.Value);

            dictCond = new Dictionary<string, object>() { { "tablename", strWebMapKeyTableName }, { "K3KeyValue", strDestKeyValue } };
            kvp = dbDestK3Con.CreateCondParam(dictCond);
            strSql = string.Format(@"delete from {0} where  {1}", "mWebK3KeyMap", kvp.Key);
            dbDestK3Con.ExecuteSql(strSql, kvp.Value);


            strSql = @"insert into mLog(logtype,keyvalue,logstatus)
                values(@logtype,@keyvalue,@logstatus)";
            dbDestK3Con.ExecuteSql(strSql, dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { 
                 {"logtype",LogType.goods_category.ToString()}
                 ,{"keyvalue",strDestKeyValue}
                 ,{"logstatus","2"}
             }).Value);


        }
        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            int iDestKey = base.Update(dbOrgWebReadCon,dbOrgWebWriteCon, dbDestK3Con, strKeyValue);

            string strFNumber=dbDestK3Con.GetValue("select fnumber from t_item where fitemid=@fitemid"
                ,new SqlParameter[]{new SqlParameter("@fitemid",iDestKey.ToString())});
            string strSql = @"update t_item set ffullname='' where fnumber like @fnumber and fitemclassid=4";
            dbDestK3Con.ExecuteSql(strSql
                , new SqlParameter[] { new SqlParameter("@fnumber", strFNumber+".%") });
            
            return iDestKey;
        }

        protected override void GetFNumber(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue, out string[] arrFnumber, out string[] arrFName, out string[] arrKeyValue)
        {

            KeyValuePair<string, IDataParameter[]> kvp = dbOrgWebReadCon.CreateCondParam(new Dictionary<string, object>() { { "FitemID", strKeyValue } });
            DataRow drOrgData = dbOrgWebReadCon.GetDataFirstRow(string.Format("select FitemID,Fparentid,Fname,Fnumber from t_item where {0}", kvp.Key), kvp.Value);

            arrFnumber =  CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
            arrFName = new string[arrFnumber.Length];
            arrKeyValue = new string[arrFnumber.Length];


            arrFName[arrFnumber.Length - 1] = CvtFunc.ToString(drOrgData["Fname"]);
            arrKeyValue[arrFnumber.Length - 1] = strKeyValue;
            for (int i = arrFnumber.Length - 2; i >= 0; i--)
            {

                int intparent_id = CvtFunc.ToInt(drOrgData["Fparentid"]);
                if (intparent_id > 0)
                {
                   
                    arrKeyValue[i] = intparent_id.ToString();

                    kvp = dbOrgWebReadCon.CreateCondParam(new Dictionary<string, object>() { { "FitemID", intparent_id.ToString() } });
                    drOrgData = dbOrgWebReadCon.GetDataFirstRow(string.Format("select FitemID,Fparentid,Fname,Fnumber from t_item  where {0}", kvp.Key), kvp.Value);

                    arrFName[i] = CvtFunc.ToString(drOrgData["Fname"]);
                }

            }
            string strFParentNumber = string.Empty;
            if (arrFnumber.Length > 1)
            {
                for (int i = 0; i < arrFnumber.Length - 1; i++)
                {
                    arrFnumber[i] = base.GetFShortNumber(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, arrKeyValue[i]);
                    strFParentNumber = string.Join(".", arrFnumber, 0, i + 1);
                }
            }
            

        }

        
    }
}